package 题目集.前缀和.二维前缀和;

import java.util.Scanner;

//https://vjudge.net/problem/%E8%AE%A1%E8%92%9C%E5%AE%A2-A1603
//前缀和
public class 天上的星星 {
    static int[][] pre=new int[2002][2002];
    static int n;

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        for (int i = 1; i <= n; i++) {
            pre[sc.nextInt()+1][sc.nextInt()+1]+=sc.nextInt();
        }
        for (int i = 1; i < pre.length; i++) {
            for (int j = 1; j < pre[i].length; j++) {
                pre[i][j]+=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1];
            }
        }
        int m=sc.nextInt();
        for (int i = 0; i < m; i++) {
            int x1=sc.nextInt()+1;
            int y1=sc.nextInt()+1;
            int x2=sc.nextInt()+1;
            int y2=sc.nextInt()+1;
            System.out.println(pre[x2][y2]-pre[x1-1][y2]-pre[x2][y1-1]+pre[x1-1][y1-1]);
        }
    }
}
